/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lloseng.ocsf.simplechat.phase1.server;

import com.langage.commande.serveur.ui.cor.AnalyseurQuit;
import com.langage.commande.serveur.ui.cor.AnalyseurStart;
import com.langage.commande.serveur.ui.cor.AnalyseurStop;
import com.langage.commande.serveur.ui.cor.ChainAnalyseurServer;
import com.lloseng.ocsf.common.ChatIF;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author Touf
 */
public class ServerConsole implements ChatIF {

    protected EchoServer serveur;

    public ServerConsole(Integer port) {
        if(port==null)
            port=EchoServer.DEFAULT_PORT;
        serveur=new EchoServer(port,this);
       
    }
/**
   * This method waits for input from the console.  Once it is 
   * received, it sends it to the client's message handler.
   */
  public void accept() 
  {
    try
    {
      BufferedReader fromConsole = 
        new BufferedReader(new InputStreamReader(System.in));
      String message;

      while (true) 
      {
        message = fromConsole.readLine();
        serveur.handleMessageFromServerUI(message);
      }
    } 
    catch (Exception ex) 
    {
      System.out.println
        ("Unexpected error while reading from console!");
    }
  }
    @Override
    public void display(String message) {
        System.out.println(message);
    }

    public final void listen() throws IOException {
        serveur.listen();
    }
    
    
    
     public static void main(String[] args) {
        Integer port = 0; //Port to listen on

        try {
            port = Integer.parseInt(args[0]); //Get port from command line
        } catch (Throwable t) {
            port = null; //Set port to 5555
        }

        ServerConsole sv = new ServerConsole(port);

//        try {
//            sv.listen(); //Start listening for connections
//        } catch (Exception ex) {
//            System.out.println("ERROR - Could not listen for clients!");
//        }
        sv.accept();
    }
}
